package demo;

import java.util.ResourceBundle;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;



public class AccountCodeValidator implements Validator {
	public static final String KEY_BAD_VALID = "message.validation.impossible";
	public void validate(FacesContext contexte, UIComponent composant,
			Object objet) throws ValidatorException {
		String valeur = null;
		boolean estValide = false;
		if ((contexte == null) || (composant == null)) 
			throw new NullPointerException();
		if (!(composant instanceof UIInput)) 
			return;
		valeur = objet.toString();
		Pattern p = Pattern.compile("[0-9][0-9]-[0-9][0-9][0-9]", 
				   Pattern.MULTILINE);
		Matcher m = p.matcher(valeur);
		estValide = m.matches();
		if (!estValide) {
			FacesMessage errMsg = MessageFactory.getMessage("i18n.Messages",contexte,
					KEY_BAD_VALID);			
			throw new ValidatorException(errMsg);
		}
	}
}


